package com.jiang.service;

import com.jiang.Domain.Message;
import com.jiang.Domain.MessageType;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author Jiang
 * @version 1.0
 * @date 2023/11/30 19:51
 * 与发送消息相关的实体类
 */
public class MessageClientService {
    public void sendMessageToOne(String content, String sendId, String getterId ) {
        // 构建message
        Message message = new Message();
        message.setContent(content);
        message.setSender(sendId);
        message.setGetter(getterId);
        message.setSendTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
        message.setMesType(MessageType.MESSAGE_COMM_MES);
        System.out.println("你对" + getterId + "说：" + content);

        // 发送给服务器
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(ManageClientConnectServerThread.getClientConnectServerThread(sendId).getSocket().getOutputStream());
            objectOutputStream.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendMessageToAll(String content, String senderId) {
        // 构建message
        Message message = new Message();
        message.setContent(content);
        message.setSender(senderId);
        message.setSendTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
        message.setMesType(MessageType.MESSAGE_TO_ALL_MES);
        System.out.println("你对所有人说：" + content);

        // 发送给服务器
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(ManageClientConnectServerThread.getClientConnectServerThread(senderId).getSocket().getOutputStream());
            objectOutputStream.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
